package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelContentService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.query.TravelQuery;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 主页控制
 */
@Controller
@RequestMapping("/travel")
public class TravelController {
    @Value("${file.dir}")
    private String fileDir;

    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelContentService travelContentService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelCommentService travelCommentService;

    @RequestMapping("/commentAdd")
    public String commentAdd(TravelComment travelComment, Integer floor,
                             @LoginUser UserInfo userInfo, Model model) {
        //设置用户信息
        travelComment.setUserId(userInfo.getId());
        travelComment.setUsername(userInfo.getNickname());
        travelComment.setLevel(userInfo.getLevel());
        travelComment.setHeadUrl(userInfo.getHeadImgUrl());
        travelComment.setCity(userInfo.getCity());
        //保存
        travelComment = travelCommentService.save(travelComment);
        //共享
        model.addAttribute("c", travelComment);
        //楼层
        model.addAttribute("floor", ++floor);
        return "travel/commentTpl";
    }

    @RequestMapping("")
    public String index(@ModelAttribute("qo") TravelQuery qo, Model model) {
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "travel/list";
    }

    @RequestMapping("/detail")
    public String detail(Long id, Model model) {
        Travel travel = travelService.selectByPrimaryKey(id);
        TravelContent content = travelContentService.get(id);
        travel.setTravelContent(content);
        model.addAttribute("detail", travel);
        //查询评论列表
        List<TravelComment> list = travelCommentService.findByTravelId(id);
        model.addAttribute("list", list);
        //吐司
        List<Destination> toasts = destinationService.toastsList(travel.getDest().getId());
        model.addAttribute("toasts", toasts);

        //sds相关攻略
        List<StrategyDetail> sds = strategyDetailService.selectByDestIdTop3(travel.getDest().getId());
        model.addAttribute("sds", sds);

        //ts相关游记
        List<Travel> ts = travelService.selectByDestIdTop3(travel.getDest().getId());
        model.addAttribute("ts", ts);
        return "travel/detail";
    }


    @RequestMapping("/input")
    @RequiredLogin
    //写游记,需要登录
    public String input(Long id, Model model) {
        //查询所有目的地
        List<Destination> dests = destinationService.selectAll();
        model.addAttribute("dests", dests);
        if (id != null) {
            Travel travel = travelService.selectByPrimaryKey(id);
            model.addAttribute("tv", travel);
        }
        return "travel/input";
    }

    @RequestMapping("/getContent")
    @ResponseBody
    public Object getContent(Long id) {
        return travelContentService.get(id);
    }


    @RequestMapping("/coverImageUpload")
    @ResponseBody
    //上传封面
    public String input(MultipartFile pic) {
        //上传封面操作
        return UploadUtil.upload(pic, fileDir);
    }


    //内容图片上传
    //UME上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        java.lang.String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, fileDir);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, @LoginUser UserInfo userInfo) {
        return travelService.saveOrUpdate(travel, userInfo);
    }
}